package com.example.demo.command;

/**
 * 遥控器
 *
 * @author Liu Huijun
 * @date 2023-02-08
 */
public class RemoteControl {

    private Command[] commandOns;

    private Command[] commandOffs;

    public void setCommandOn(int index, Command com){
        if (commandOns == null) {
            commandOns = new Command[10];
            for (int i = 0; i < commandOns.length; i++) {
                commandOns[i] = new NoCommand();
            }
        }
        commandOns[index] = com;
    }

    public void setCommandOff(int index, Command com){
        if (commandOffs == null) {
            commandOffs = new Command[10];
            for (int i = 0; i < commandOffs.length; i++) {
                commandOffs[i] = new NoCommand();
            }
        }
        commandOffs[index] = com;
    }

    public void on(int index){
        commandOns[index].execute();
    }

    public void off(int index){
        commandOffs[index].execute();
    }

    public void on(){
        for (Command commandOn : commandOns) {
            commandOn.execute();
        }
    }

    public void off(){
        for (Command commandOff : commandOffs) {
            commandOff.execute();
        }
    }

}
